package com.scoy.honeymei.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.viewpager.widget.ViewPager;

import com.scoy.honeymei.R;
import com.scoy.honeymei.utils.MyUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:      com.shanchuang.propertycustomer.dialog
 * Description:
 * Author:         GaoZhan & Administrator
 * UpdateDate:     2019/12/31/0031$ 上午 10:22$
 * UpdateRemark:   更新内容
 */
public class ShowFullDialog extends DialogFragment {


    /**
     *  ShowFullDialog.newInstance(images).show(getSupportFragmentManager(), pos + "");
     */

    private ArrayList<String> imglist;
    private int currentPos;
    private ViewPager imgVp;
    private TextView imgNumIv;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            imglist = getArguments().getStringArrayList("imglist");
        }
    }

    public static ShowFullDialog newInstance(List<String> imglist) {
        Bundle args = new Bundle();
        args.putStringArrayList("imglist", (ArrayList<String>) imglist);
        ShowFullDialog fragment = new ShowFullDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题
        View view = inflater.inflate(R.layout.dialog_fullscreen, null, false);

        imgNumIv = view.findViewById(R.id.img_num_iv);
        imgVp = view.findViewById(R.id.img_vp);

        initView();
        return view;
    }

    private void initView() {

        MyUtil.setStatusBar(getContext(), getDialog().getWindow(), true, R.color.black);

        currentPos = Integer.parseInt(getTag() == null ? "0" : getTag());

        initVP();

    }


    @Override
    public void onStart() {
        super.onStart();
        Window dialogWindow = getDialog().getWindow();
        if (dialogWindow != null) {
            dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
            dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialogWindow.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.BOTTOM;
            lp.windowAnimations = android.R.style.Animation_InputMethod;
            dialogWindow.setAttributes(lp);
        }
    }

    private void initVP() {

        ImgVPAdapter vpAdapter = new ImgVPAdapter(getContext(), imglist);
        imgVp.setAdapter(vpAdapter);
        imgVp.setCurrentItem(currentPos);
        imgNumIv.setText((currentPos + 1) + "/" + imglist.size());


        vpAdapter.setAllClickListener(pos -> {
            dismiss();
        });


        imgVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(final int position) {
                imgNumIv.setText((position + 1) + "/" + imglist.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
